package com.linkoog.devtools.code_generator.setting.persistent;

import com.linkoog.devtools.code_generator.database.domain.ConnectionInfo;
import com.linkoog.devtools.code_generator.database.domain.DbType;
import com.linkoog.devtools.code_generator.database.domain.TypeMapping;
import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.linkoog.devtools.code_generator.setting.persistent.state.CodeGenDevToolsState;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 */
@State(name = "CodeGeneratorDevToolsPersistent", storages = @Storage("DevTools-CodeGenerator.xml"))
public class CodeGenDevToolsPersistent implements PersistentStateComponent<CodeGenDevToolsState> {

    private CodeGenDevToolsState myState = new CodeGenDevToolsState();

    @NotNull
    public static CodeGenDevToolsPersistent getInstance() {
        return ServiceManager.getService(CodeGenDevToolsPersistent.class);
    }

    @Nullable
    @Override
    public CodeGenDevToolsState getState() {
        return myState;
    }

    @Override
    public void loadState(CodeGenDevToolsState state) {
        XmlSerializerUtil.copyBean(state, myState);
    }

    public List<ConnectionInfo> getConnectionInfoList() {
        return myState.getConnectionInfoList();
    }

    public void setConnectionInfoList(List<ConnectionInfo> connectionInfoList) {
         myState.setConnectionInfoList(connectionInfoList);
    }

    public List<TypeMapping> getTypeMappings() {
        return myState.getTypeMappings();
    }

    public Map<String,String> getTypeMappings(@NotNull DbType dbType) {
        Map<String,String> mapping = new LinkedHashMap<>();
        for (TypeMapping typeMapping : myState.getTypeMappings()){
            if (typeMapping.getDbType().equals(dbType) && typeMapping.isChecked()){
                mapping.put(typeMapping.getColumnDataType(), typeMapping.getJavaFieldType());
            }
        }
        return mapping;
    }

    public void setTypeMappings(List<TypeMapping> typeMappings) {
        myState.setTypeMappings(typeMappings);
    }

    public List<TemplateNodeData> getTemplateDataList() {
        return myState.getTemplateDataList();
    }

    public void setTemplateDataList(List<TemplateNodeData> templateDataList) {
        List<TemplateNodeData> list = new ArrayList<>(templateDataList.size());
        for (TemplateNodeData templateNodeData : templateDataList) {
            list.add(templateNodeData.deepCopy());
        }
        this.myState.setTemplateDataList(list);
    }

    public Integer nextConnNameIndex() {
        Integer index = myState.getConnNameIndex() + 1;
        myState.setConnNameIndex(index);
        return index;
    }

}
